<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

/**
 * 个人日程管理
 */

class EventController extends BaseController {

	public function index(){
		$psize = 12;
        // $psize = I('get.psize', $this->m_psize,'intval');
        $keys = I('get.keys','');
        $id = I('get.id','');
        $s_title = I('get.title','','trim');
        $s_owner = I('get.owner','','trim');
        $s_starttime = I('starttime','','trim');
        $this_month = date('Y-m');
        $s_by = I('by','','trim');
		if( !$psize )
			$psize = $this->m_psize;
		else if( $psize > 12 )
			$psize = $this->m_psize;

		$MEvent = D("Event");
        $MUser = D("User");
        $Sector = M('Sector');
        $Role = M('Role');

		$wheres = array();
		$wheres['_string'] = '1=1';
		$wheres['sector.is_deleted'] = array('neq',1);
        $wheres['event.is_deleted'] = array('neq', 1);
        $wheres['user.is_deleted'] = array('neq', 1);
        if( $s_owner )      
            $wheres['user.nickname'] = array('like','%'.$s_owner.'%');  //按照用户昵称：nickname查询
        if( $s_title )
            $wheres['event.title'] = array('like','%'.$s_title.'%');
        if($s_starttime)
            $wheres['event.starttime'] = array('like','%'.$s_starttime.'%');
        if($keys == 1)              //我负责的
            $wheres['event.owner_id'] = array('eq',$this->m_user['user_id']);
        elseif($keys == 2){         //我下属负责的
            $wheres['event.owner_id'] = array('neq',$this->m_user['user_id']);  //去掉用户自己日志
            $map['is_deleted'] = array('neq',1);
            $array = $Sector->field("sector_id,sector_name,parent_sector") -> where($map)->select();
            $arr = list_to_sector($array,$this->m_user['sector_id']);
            foreach ($arr as $key => $value) {                  //new_arr：当前用户部门+下属部门列表
                $new_arr[] = $value['sector_id'];
            }
            $wheres['event.owner_sector'] = array('in',$new_arr);
            $maps = array();
            $maps['parent_role'] = array('eq',$this->m_user['role_id']);
            $find_res = $Role ->field("role_id,parent_role")->where($maps)->find();
            if(empty($find_res)){               //若当前用户无下属岗位，则为普通员工，下属负责内容为空
              $res['rcode'] = 2;
              $res['msg'] = "操作日志列表为空";
              e_json($res);
            }
          }
        else if($keys == 3 )    //我创建的
            $wheres['event.creator_id'] = array('eq',$this->m_user['user_id']);
        if( $id == "today"  )
            $wheres['event.starttime'] = array('like',date('Y-m-d').'%');
        else if($id == "month" )
            $wheres['event.starttime'] = array('like','%'.$this_month.'%');
        else if($id == "week")
            $wheres['event.starttime'] = array('between',$MEvent->getWeek());

        $cnt = $MUser->join("event on event.owner_id=user.user_id")
                     ->join("sector on sector.sector_id=user.sector_id")
                     ->where( $wheres )->count();
        $Page = new \Common\ORG\Page( $cnt, $psize );
		    $res = array('rcode'=>1,'data'=>NULL, 'msg'=> 'OK');
        $dlist = $MUser
            ->field("event.event_id,event.title,event.owner_id,event.creator_id,event.starttime,event.endtime,user.nickname,
                    event.location,event.create_time,event.update_time,user.user_id,event.content")
            ->join("event on event.owner_id=user.user_id")
            ->join("sector on sector.sector_id=user.sector_id")
            ->where( $wheres )
            ->order("event.starttime desc ")
            ->limit($Page->firstRow.','.$Page->listRows )
            ->select();
        $sql = $MUser -> getLastSql();
		if( $dlist ){
  			$res['rcode'] = 1;
            $res['sql'] = $sql;
  			$res['data'] = $dlist;
  			$res['pall'] =  $Page->getTotalPages();
  			$res['p'] = $Page->nowPage;
  			$res['psize'] = $psize;
            $res['wheres'] = $wheres;
		}
        else{
            $res['rcode'] = 2;
            $res['sql'] = $sql;
			$res['msg'] = "日程内容为空";
		}
		e_json($res);
	}

    //负责人列表
    public function showUsers(){
        $psize = I('get.psize', $this->m_psize,'intval');
        $sector_id = I('get.sectors','','intval');
        $nickname = I('get.nickname','','trim');
        $wheres = array();
        $wheres['_string'] = '1=1';
        $wheres['user.is_deleted'] = array('neq', 1);
        $wheres['sector.is_deleted'] = array('neq', 1);
        $MEvent = D("Event");
        $MUser = D("User");
        $Sector = M('Sector');
        $Role = M('Role');
        $map['is_deleted'] = array('neq',1);
        $arrays = $Sector->field("sector_id,sector_name,parent_sector") -> where($map)->select();

        if($nickname)
            $wheres['user.nickname'] = array('like','%'.$nickname.'%');
        if($sector_id){    //根据部门选择日程负责人时，若部门存在下属部门，则显示
            $arr = list_to_sector($arrays,$sector_id);
            foreach ($arr as $key => $value) {                      //sector_list：所搜索部门+下属部门
                $sector_list[] = $value['sector_id'];
            }
            $wheres['sector.sector_id'] = array('in',$sector_list);
        }

        if( !$psize )
            $psize = $this->m_psize;
        else if( $psize > 20 )
            $psize = $this->m_psize;

        $arr_sectorlist = list_to_sector($arrays,$this->m_user['sector_id']);
        foreach ($arr_sectorlist as $key => $value) {
            $new_sector_list[] = $value['sector_id'];               //当前用户的部门+下属部门列表
        }
        $maps = array();
        $maps['parent_role'] = array('eq',$this->m_user['role_id']);
        $find_res = $Role ->field("role_id,parent_role")->where($maps)->find();
        if(empty($find_res))                //若当前用户无下属岗位，则为普通员工，负责人列表则为员工自己
            $wheres['user.user_id'] = array('eq', $this->m_user['user_id'] );
        else 
            $wheres['user.sector_id'] = array('in', $new_sector_list );

        $cnt = $MUser
            ->join("sector on sector.sector_id = user.sector_id")
            ->join("role on user.role_id=role.role_id")
            ->where( $wheres )
            ->count();

        $Page = new \Common\ORG\Page( $cnt, $psize );
        $res = array('rcode'=>1,'data'=>NULL, 'msg'=> 'OK');
        $dlist = $MUser
            ->field("user.login_name,sector.sector_id,sector.sector_name,role.role_name,user.email,user.phone,user.user_id,user.nickname")
            ->join("sector on user.sector_id=sector.sector_id")
            ->join("role on user.role_id=role.role_id")
            ->where( $wheres )
            ->order("user.user_id desc ")
            ->limit($Page->firstRow.','.$Page->listRows )
            ->select();
        $sql = $MUser -> getLastSql();
        if( $dlist ){
            $res['rcode'] = 1;
            $res['data'] = $dlist;
            $res['roles_list'] = $roles_list;
            $res['nickname'] = $nickname;
            $res['pall'] =  $Page->getTotalPages();
            $res['p'] = $Page->nowPage;
            $res['psize'] = $psize;
            $res['sector_id'] = $sector_id;
        }else{
            $res['rcode'] = 2;
            $res['roles_list'] = $roles_list;
            $res['msg'] = "负责人列表为空";
            $res['sector_id'] = $sector_id;
       }
          e_json ($res);
    }
       
	//获取当前用户的下属部门名称列表
    public function getSector(){
        $MEvent = D("Event");
        $uinfo = $MEvent -> getUinfo();
    	$Sectors=M("Sector");
        $wheres = array();

        $where['sector_id']  = array('eq', $this->m_user['sector_id']);
        $where['parent_sector']  = array('eq', $this->m_user['sector_id']);
        $where['_logic'] = 'or';
        $map['_complex'] = $where;
        $map['is_deleted']  = array('neq',1);
        $map['sector_id'] = array('neq',0);
         
    	$slist = $Sectors->field('sector_id,sector_name')->where($map)->select();
    	e_json( array("psector"=>array_values($slist)));
    }


    public function storOwner(){
        $res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
        $user_id = I('get.strdelid',false,"intval");
        $wheres = array();
        $wheres['_string'] = '1=1';
        $wheres['is_deleted'] = array('neq', 1);
        if($user_id) {
            $wheres['user_id'] = array('eq', $user_id);
        }

        $MUser = D("User");
        $userdata = $MUser ->field("user_id,nickname")->where($wheres) -> find();
        $sql = $MUser -> getLastSql();
        if($userdata){
            $res['rcode'] = 1;
            $res['msg'] = 'OK';
            $res['data'] = $userdata;
        }else{
            $res['rcode'] = 2;
            $res['msg'] = "负责人为空";
        }
        e_json($res);
    }
	public function add(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$MEvent = D('Event');
		$title = I('post.title',false,'trim');
		$content = I('post.content',false,'trim');
        $location = I('post.location',' ','trim');
        $owner = I('post.owner','','trim');
        $owner_id = I('post.owner_id','','intval');
        $starttime = I('post.starttime','','trim');
        $endtime = I('post.endtime','','trim');
        if($owner_id){
            $MUser = M('User');
            $wheress = array();
            $wheress['user_id'] = array('eq',$owner_id);
            $userinfo = $MUser ->where($wheress)->find();
            $owner_sector = $userinfo['sector_id'];
            $owner_role = $userinfo['role_id'];
            $addres = $MEvent->add_event( $title, $content, $location, $owner, $owner_id, $owner_sector, $owner_role, $starttime, $endtime);
        }
		
		if( $addres['r'] ){
			$res['rcode'] = 1;
			$res['msg'] = 'OK';
			$res['data'] = $addres['d'];
            $res['owner'] = $addres['owner'];
		} else{
			$res['msg'] = $addres['i'];
		}
		e_json( $res );
	}

	public function edit(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$MEvent = D('Event');
		$event_id = I('post.event_id',false,'intval');
		$title = I('post.title',false,'trim');
		$content = I('post.content',false,'trim');
		$location = I('post.location',false,'trim');
        $owner = I('post.owner','','trim');
        $owner_id = I('post.owner_id','','intval');
        $starttime = I('post.starttime','','trim');
        $endtime = I('post.endtime','','trim');
		$addres = $MEvent->update_event($event_id, $title, $content, $location,$owner,$owner_id,$starttime,$endtime);
		if( $addres['r'] ){
			$res['rcode'] = 1;
			$res['msg'] = 'OK';
			$res['data'] = $addres['d'];
		}
		else{
			$res['msg'] = $addres['i'];
		}
		e_json( $res );
	}

	public function remove(){
		$id = I('get.id',false,'intval');
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		if( !$id ){
			$res['msg'] = '参数错误';
		}
		else{
			$MEvent = D('Event');
			$delres = $MEvent->delete_event( $id );
			if( $delres['r']){
				$res['rcode'] = 1;
				$res['msg'] = '删除成功';
			}else{
				$res['msg'] = $delres['i'];
			}
		}
		e_json( $res );
	}

}